//
//  ALPS_ClassifyController.m
//  Alps
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_ClassifyController.h"
#import "ALPS_ClassifyModel.h"
#import "ALPS_ClassifyCollectionItem.h"
// 头视图5个item
#import "ALPS_CollectionViewHeader5Item.h"
// 详情界面
#import "ALPS_ClassifyClickinController.h"

@interface ALPS_ClassifyController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

/** 数据源数组 */
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
/** 集合视图 */
@property (nonatomic, strong) UICollectionView *collectionView;
/** 监控head5 tap的是第几个 */
@property (nonatomic, assign) NSInteger num;

@end

@implementation ALPS_ClassifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.000];
    
    // 数据源数组是初始化
    self.dataSourceArr = [NSMutableArray array];
    [self getData];
    
    // 创建collectionView
    [self createCollectionView];
    
    [self defaultCenterPushCollectVC];
}

#pragma mark -- 请求集合视图数据
- (void)getData
{
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=iPhone&picVersion=12&scale=2";
    [WSSNetTool GetNetByURL:urlStr body:nil headers:nil response:WSSJSON success:^(id result) {
        for (NSDictionary *dic in result[@"list"])
        {
            ALPS_ClassifyModel *model = [[ALPS_ClassifyModel alloc] initWithDic:dic];
            [self.dataSourceArr addObject:model];
        }
        
        // 刷新UI
        if (self.dataSourceArr.count > 0)
        {
            [self.collectionView reloadData];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error.description);
    }];
}

#pragma mark -- 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((ScreeWidth - 15) / 2, 40);
    flowLayout.minimumLineSpacing = 3;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64 - 30 ) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.000];
    [self.view addSubview:self.collectionView];
    
    // 注册item
    [self.collectionView registerClass:[ALPS_ClassifyCollectionItem class] forCellWithReuseIdentifier:@"ALPS_ClassifyCollectionItem"];
    // 注册头视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classifyheader"];
}

#pragma mark collectionView代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArr.count - 5 - 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_ClassifyCollectionItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"ALPS_ClassifyCollectionItem" forIndexPath:indexPath];
    item.model = [self.dataSourceArr objectAtIndex:(indexPath.row + 5)];
    return item;
}

#pragma mark 头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classifyheader" forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.000];
    
    // 在头视图上添加5个item
    CGFloat ViewWidth = ScreeWidth / 5.0;
    for (NSInteger i = 0; i < 5; i++)
    {
        ALPS_CollectionViewHeader5Item *itemView = [[ALPS_CollectionViewHeader5Item alloc] initWithFrame:CGRectMake(ViewWidth * i, 0, ViewWidth, ViewWidth + 30)];
        if (self.dataSourceArr.count > 5)
        {
            itemView.tag = 1000 + i;
            itemView.model = [self.dataSourceArr objectAtIndex:i];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapHeader5Item:)];
            [itemView addGestureRecognizer:tap];
        }
        [headerView addSubview:itemView];
    }
    return headerView;
}

#pragma mark 头视图的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreeWidth, ScreeWidth / 5.0 + 30);
}

#pragma mark 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_ClassifyClickinController *clickinVc = [[ALPS_ClassifyClickinController alloc] init];
    ALPS_ClassifyModel *model = [self.dataSourceArr objectAtIndex:(indexPath.row + 5)];
    clickinVc.id = model.id;
    clickinVc.navigationItem.title = model.title;
    [self.navigationController pushViewController:clickinVc animated:YES];
}
#pragma mark 上面5个item的轻拍手势
- (void)didTapHeader5Item:(UITapGestureRecognizer *)tap
{
    ALPS_ClassifyClickinController *clickinVc = [[ALPS_ClassifyClickinController alloc] init];
    
    ALPS_ClassifyModel *model = [self.dataSourceArr objectAtIndex:tap.view.tag - 1000];
    clickinVc.id = model.id;
    [self.navigationController pushViewController:clickinVc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
